package com.mud.dnd.frontend.web.mbeans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Action;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.domain.model.CardAction;
import com.mud.dnd.domain.model.Comment;
import com.mud.dnd.domain.model.Deck;
import com.mud.dnd.ejb.ActionManager;
import com.mud.dnd.ejb.CardManager;
import com.mud.dnd.ejb.CommentManager;
import com.mud.dnd.ejb.DeckManager;



public class AbstractCardsBean {
	

	@EJB
	protected CardManager cardManager;
	
	@EJB
	protected DeckManager deckManager;
	
	@EJB
	protected ActionManager actionManager;
	
	@EJB
	protected CommentManager commentManager;
	
	
	protected List<Deck> decks;
	protected List<Card> cards;
	protected List<Card> allCards;
	protected List<Action> actions;
	protected List<Action> allActions;
	protected List<CardAction> cardActions;
	protected List<Comment> comments;
	
	protected Card selectedCard;
	protected int selectedCardRate;
	protected Deck selectedDeck;
	protected Action selectedAction;
	protected int selectedActionAmount;
	
	protected String searchDeckName;
	protected String searchDeckDesc;
	protected String deckOutput;
	
	protected String searchCardName;
	protected String searchCardDesc;
	protected String cardOutput;
	
	protected String searchActionName;
	protected String searchActionDesc;
	protected String actionOutput;
	
	
	
	
	
	
	public AbstractCardsBean() {
		actions = new ArrayList<Action>();
		cardActions = new ArrayList<CardAction>();
		cards = new ArrayList<Card>();
	}


	public void selectDeck(Deck deck)
	{
		selectedDeck = deck;
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		allCards = cardManager.getCards(params);
		
		try {
			params.add(new QueryParameter("deckID",QueryParamOperator.EQUALS,selectedDeck.getId()));
		} catch (DAOException e) {
			cardOutput = e.getMessage();
		}
		
		cards = cardManager.getCards(params);
	}
	
	
	public void searchDeck()
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try {
			if(searchDeckName!=null && (!searchDeckName.equals("")) )
			{
				params.add(new QueryParameter("name",QueryParamOperator.LIKE,searchDeckName));
			}
			if(searchDeckDesc!=null && (!searchDeckDesc.equals("")))
			{
				params.add(new QueryParameter("description",QueryParamOperator.LIKE,searchDeckDesc));
			}
		} catch (DAOException e) {
			deckOutput = e.getMessage();
		}
		
		decks = deckManager.getDecks(params);
	}
	
	public void selectCard(Card card)
	{
		
		
		selectedCard = card;
		//refreshCard();
		refreshComments();
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		allActions = actionManager.getActions(params);
		
		/*try {
			params.add(new QueryParameter("cardID",QueryParamOperator.EQUALS,selectedCard.getId()));
		} catch (DAOException e) {
			actionOutput = e.getMessage();
		}
		
		actions = actionManager.getActions(params);*/
		
		searchActionInCard(actions,cardActions);
		
		
	}
		
	
	public void searchCardInDeck(List<Card> cardList)
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		
		try {
			params.add(new QueryParameter("deckID",QueryParamOperator.EQUALS,selectedDeck.getId()));
		} catch (DAOException e) {
			showErrorMessage(e.getMessage());
		}
		
		searchCard(cardList,params);
	
	}
	
	public void searchCard(List<Card> cardList)
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		searchCard(cardList,params);
		
	}
	
	public void searchCard(List<Card> cardList,List<QueryParameter> params)
	{
		
		try {
			if(searchCardName!=null && (!searchCardName.equals("")) )
			{
				params.add(new QueryParameter("name",QueryParamOperator.LIKE,searchCardName));
			}
			if(searchCardDesc!=null && (!searchCardDesc.equals("")) )
			{
				params.add(new QueryParameter("description",QueryParamOperator.LIKE,searchCardName));
			}
			
			
		} catch (DAOException e) {
			cardOutput = e.getMessage();
		}
		
		cardList.clear();
		cardList.addAll(cardManager.getCards(params));
	}
	
	public void refreshCard()
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try {
			params.add(new QueryParameter("id",QueryParamOperator.EQUALS,selectedCard.getId()));
		} catch (DAOException e) {
			cardOutput = e.getMessage();
		}
		
		selectedCard = cardManager.getCards(params).get(0);
		selectedCardRate = (int) Math.round(selectedCard.getRate());
	}
	
	
	public void selectAction(CardAction action)
	{
		
		selectedActionAmount = action.getAmount();
		selectAction(action.getAction());
		
		
	}
	
	
	public void selectAction(Action action)
	{
		
		
		selectedAction = action;
		refreshAction();

	}
		
	
	public void searchAction(List<Action> actionList, List<CardAction> cardActionList)
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		searchAction(actionList,cardActionList,params);
	}
	
	public void searchActionInCard(List<Action> actionList, List<CardAction> cardActionList)
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try {
			
			
			params.add(new QueryParameter("cardID",QueryParamOperator.EQUALS,selectedCard.getId()));
		} catch (DAOException e) {
			actionOutput = e.getMessage();
		}
		
		searchAction(actionList,cardActionList,params);
	}
	
	public void searchAction(List<Action> actionList, List<CardAction> cardActionList,List<QueryParameter> params)
	{
		try {
			if(searchActionName!=null && (!searchActionName.equals("")) )
			{
				params.add(new QueryParameter("name",QueryParamOperator.LIKE,searchActionName));
			}
			if(searchActionDesc!=null && (!searchActionDesc.equals("")) )
			{
				params.add(new QueryParameter("description",QueryParamOperator.LIKE,searchActionDesc));
			}
			
			
		} catch (DAOException e) {
			actionOutput = e.getMessage();
		}
		
		actionList.clear();
		actionList.addAll( actionManager.getActions(params));
		if(cardActionList!=null)
		{
			cardActionList.clear();
			cardActionList.addAll(actionManager.getCardActions(params));
			Collections.sort(cardActionList);
		}
	}
	
	public void refreshAction()
	{
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try {
			params.add(new QueryParameter("id",QueryParamOperator.EQUALS,selectedAction.getId()));
		} catch (DAOException e) {
			cardOutput = e.getMessage();
		}
		
		selectedAction = actionManager.getActions(params).get(0);
		//selectedCardRate = (int) Math.round(selectedCard.getRate());
	}
	
	public void refreshComments()
	{
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try {
			params.add(new QueryParameter("gameItemID",QueryParamOperator.EQUALS,selectedCard.getId()));
		} catch (DAOException e) {
			cardOutput = e.getMessage();
		}
		
		comments = commentManager.getComments(params);
	}
		

	protected void showErrorMessage(String msg)
	{
		
		 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", msg);  
		 FacesContext.getCurrentInstance().addMessage(null, message);  
	}
	
	

	
	
	public List<Deck> getDecks() {
		return decks;
	}
	public void setDecks(List<Deck> decks) {
		this.decks = decks;
	}
	public List<Card> getCards() {
		return cards;
	}
	public void setCards(List<Card> cards) {
		this.cards = cards;
	}
	public Card getSelectedCard() {
		return selectedCard;
	}
	public List<Comment> getComments() {
		return comments;
	}
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}
	public void setSelectedCard(Card selectedCard) {
		this.selectedCard = selectedCard;
	}
	public Deck getSelectedDeck() {
		return selectedDeck;
	}
	public void setSelectedDeck(Deck selectedDeck) {
		this.selectedDeck = selectedDeck;
	}
	public String getSearchDeckName() {
		return searchDeckName;
	}
	public void setSearchDeckName(String searchDeckName) {
		this.searchDeckName = searchDeckName;
	}
	public String getSearchDeckDesc() {
		return searchDeckDesc;
	}
	public void setSearchDeckDesc(String searchDeckDesc) {
		this.searchDeckDesc = searchDeckDesc;
	}
	public String getSearchCardName() {
		return searchCardName;
	}
	public void setSearchCardName(String searchCardName) {
		this.searchCardName = searchCardName;
	}
	public String getSearchCardDesc() {
		return searchCardDesc;
	}
	public void setSearchCardDesc(String searchCardDesc) {
		this.searchCardDesc = searchCardDesc;
	}
	public String getDeckOutput() {
		return deckOutput;
	}
	public void setDeckOutput(String deckOutput) {
		this.deckOutput = deckOutput;
	}
	public String getCardOutput() {
		return cardOutput;
	}
	public void setCardOutput(String cardOutput) {
		this.cardOutput = cardOutput;
	}


	public int getSelectedCardRate() {
		return selectedCardRate;
	}


	public void setSelectedCardRate(int selectedCardRate) {
		this.selectedCardRate = selectedCardRate;
	}


	public List<Card> getAllCards() {
		return allCards;
	}


	public void setAllCards(List<Card> allCards) {
		this.allCards = allCards;
	}


	public List<Action> getActions() {
		return actions;
	}


	public void setActions(List<Action> actions) {
		this.actions = actions;
	}


	public List<Action> getAllActions() {
		return allActions;
	}



	public Action getSelectedAction() {
		return selectedAction;
	}


	public void setSelectedAction(Action selectedAction) {
		this.selectedAction = selectedAction;
	}


	public String getSearchActionName() {
		return searchActionName;
	}


	public void setSearchActionName(String searchActionName) {
		this.searchActionName = searchActionName;
	}


	public String getSearchActionDesc() {
		return searchActionDesc;
	}


	public void setSearchActionDesc(String searchActionDesc) {
		this.searchActionDesc = searchActionDesc;
	}


	public String getActionOutput() {
		return actionOutput;
	}


	public void setActionOutput(String actionOutput) {
		this.actionOutput = actionOutput;
	}


	public void setAllActions(List<Action> allActions) {
		this.allActions = allActions;
	}


	public int getSelectedActionAmount() {
		return selectedActionAmount;
	}


	public void setSelectedActionAmount(int selectedActionAmount) {
		this.selectedActionAmount = selectedActionAmount;
	}


	public List<CardAction> getCardActions() {
		return cardActions;
	}


	public void setCardActions(List<CardAction> cardActions) {
		this.cardActions = cardActions;
	}
	
	
	
	
}
